//
//  TMCommityDetailHeader.m
//  国学商家管理
//
//  Created by Tao Li on 2018/7/5.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMCommityDetailHeader.h"
#import "TMCourseDetailModel.h"
#import "UIImage+TMimageCloor.h"
#import "SDCycleScrollView.h"
@interface TMCommityDetailHeader()<SDCycleScrollViewDelegate>
@property(nonatomic, strong) SDCycleScrollView                     *adView;/**< <##> */
@end
@implementation TMCommityDetailHeader
-(void)setHeadModel:(TMCourseDetailModel *)headModel
{
    _headModel = headModel;
    self.courseName.text = headModel.course;
    if ([TMUserManager deaufatManager].userModel.token == nil || headModel.rank == 0 || headModel.rank == 1) {
        self.coursePrice.text = [NSString stringWithFormat:@"¥%.02f",headModel.price];
        self.linview.hidden = YES;
        self.vipPriceLabel.hidden = YES;
        self.coursePrice.textColor = [UIColor hx_colorWithHexRGBAString:@"#FE7815"];
    }else{
        self.linview.hidden = NO;
        self.vipPriceLabel.hidden = NO;
        self.coursePrice.textColor = [UIColor hx_colorWithHexRGBAString:@"#999999"];
        self.coursePrice.text = [NSString stringWithFormat:@"¥%.02f",headModel.price];
        self.vipPriceLabel.text = [NSString stringWithFormat:@"¥%.02f",headModel.agio_price];
    }
    //bespeak 0不可预约 1可预约 2已预约
    if (self.headModel.bespeak == 0) {
        [self.canOrderBtn setTitle:@"不可预约" forState:UIControlStateNormal];
    }
    if (self.headModel.bespeak == 1) {
        [self.canOrderBtn setTitle:@"可预约" forState:UIControlStateNormal];
    }
    if (self.headModel.bespeak == 2) {
        [self.canOrderBtn setTitle:@"已预约" forState:UIControlStateNormal];
    }//FE7815
    self.collectBtn.selected = self.headModel.coll;
    self.teacherLabel.text = [NSString stringWithFormat:@"任课老师:%@", self.headModel.tea_name];
    self.haveSeatLabel.text = [NSString stringWithFormat:@"剩余座位:%ld个", self.headModel.surplus];
    self.courseType.text = [NSString stringWithFormat:@"课程分类:%@", self.headModel.type_name];
    self.openTimeLabel.text = [NSString stringWithFormat:@"开课时间:%@", [SPToolSet stringFromDateString:self.headModel.s_time]];
    self.endTimeLabel.text = [NSString stringWithFormat:@"结课时间:%@",
                              [SPToolSet stringFromDateString:self.headModel.end_time]];
    self.totalTimeLabel.text = [NSString stringWithFormat:@"总课长:%ld节", self.headModel.course_num];
    self.addressLabel.text = [NSString stringWithFormat:@"地址:%@", self.headModel.addr];
    self.courseImage.contentMode = UIViewContentModeScaleAspectFill;
    self.courseImage.clipsToBounds = YES;
    [self.courseImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithBaseUrl:self.headModel.pic[0]]] placeholderImage:kPlaceholder completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//        UIImage *images = [UIImage imageCompressForSize:image targetSize:CGSizeMake(kWindowW, 152*kWindowW/375)];
//        self.courseImage.image = images;
    }];
    
    //配置滑动
    if (!_adView) {
        _adView = [SDCycleScrollView cycleScrollViewWithFrame:self.courseImage.bounds delegate:self placeholderImage:nil];
        _adView.backgroundColor = [UIColor whiteColor];
    }
    NSMutableArray * imageURLStringsGroup= NSMutableArray.new;
    for (NSString * name in self.headModel.pic) {
        NSString * url =    [NSString stringWithBaseUrl:name];
        [imageURLStringsGroup addObject:url];
    }
    _adView.imageURLStringsGroup = imageURLStringsGroup;
    [_adView removeFromSuperview];
    [self addSubview:_adView];
    
    if (self.headModel.coll == 1) {
        self.collectBtn.selected = YES;
    }else{
        self.collectBtn.selected = NO;
    }
    if (UIScreen.mainScreen.bounds.size.width < 330) {
        NSInteger font = 10;
        self.teacherLabel.font = [UIFont systemFontOfSize:font];
        self.haveSeatLabel.font = [UIFont systemFontOfSize:font];
        self.courseType.font = [UIFont systemFontOfSize:font];
        
        self.openTimeLabel.font = [UIFont systemFontOfSize:font];
        self.endTimeLabel.font = [UIFont systemFontOfSize:font];
        self.totalTimeLabel.font = [UIFont systemFontOfSize:font];
    }
}
- (IBAction)canUseBtnClick:(UIButton *)sender {
    if (self.discountBlock) {
        self.discountBlock(1);
    }
}
- (IBAction)canOrderBtnClick:(UIButton *)sender {
    if (self.headModel.bespeak == 0 || self.headModel.bespeak == 2) {
        return;
    }
    sender.selected = !sender.selected;
    if (self.discountBlock) {
        self.discountBlock(2);
    }
}
- (IBAction)collectionBtnClick:(UIButton *)sender {
    if ([TMUserManager deaufatManager].userModel.token == nil) {
        MUST_LOGIN_ALERT;
        return;
    }
    sender.selected = !sender.selected;
    if (self.block) {
        self.block(self.headModel.cid);
    }
}
- (void)awakeFromNib{
    [super awakeFromNib];
    self.canUseBtn.layer.borderWidth = 1.0f;
    self.canUseBtn.layer.borderColor = [UIColor hx_colorWithHexRGBAString:@"#10B792"].CGColor;
    self.canUseBtn.layer.cornerRadius = 3;
    self.canUseBtn.layer.masksToBounds = YES;
    
    self.canOrderBtn.layer.borderWidth = 1.0f;
    self.canOrderBtn.layer.borderColor = [UIColor hx_colorWithHexRGBAString:@"#10B792"].CGColor;
    self.canOrderBtn.layer.cornerRadius = 3;
    self.canOrderBtn.layer.masksToBounds = YES;
}
-(void)setIsdisconut:(BOOL)isdisconut{
    _isdisconut = isdisconut;
    if (_isdisconut == YES) {
        self.buttomConst.constant = 0;
        self.buttomView.hidden = YES;
        self.canUseBtn.hidden = YES;
        self.canOrderBtn.hidden = YES;
        self.heightConst.constant = 155-90;
    }else{
        self.buttomConst.constant = 90;
        self.heightConst.constant = 155;
        self.buttomView.hidden = NO;
        self.canUseBtn.hidden = NO;
        self.canOrderBtn.hidden = NO;
    }
    [self layoutSubviews];
}
@end
